package com.lancoo.znbkxx.teacher.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseVMActivity
import com.lancoo.znbkxx.teacher.databinding.ActivityExpandTrainBinding
import com.lancoo.znbkxx.teacher.model.data.ClassicWrongQue
import com.lancoo.znbkxx.teacher.model.data.RecQuesID
import com.lancoo.znbkxx.teacher.model.data.WrongQues
import com.lancoo.znbkxx.teacher.ui.fragment.AddQuesFragment
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import com.lancoo.znbkxx.teacher.viewmodel.HardSolutionViewModel

import kotlinx.android.synthetic.main.activity_expand_train.*

/**
 * Created by wealive on 2021/3/17.
 *
 *
 */
class AddWrongQuesActivity : BaseVMActivity<HardSolutionViewModel, ActivityExpandTrainBinding>(),
        View.OnClickListener {
    private val typeNo by lazy { intent.getStringExtra("typeNo") }
    private val genreID by lazy { intent.getStringExtra("genreID") }
    private val selectedIDs by lazy { intent.getParcelableArrayListExtra<ClassicWrongQue>("ids") }

    override fun setLayoutID(): Int {
        return R.layout.activity_expand_train
    }

    override fun providerVMClass(): Class<HardSolutionViewModel>? {
        return HardSolutionViewModel::class.java
    }

    override fun initView() {
        tv_title.text = "典型错题"
        iv_back.setOnClickListener(this)
        tv_add.setOnClickListener(this)
        loadingDialog.show()
        viewModel.getChangeClassicWrongQues(Ext.STU_ID, typeNo, genreID)
    }

    override fun startObserve() {
        viewModel.classicWrongQue.observe(this, Observer { it ->
            val quesIDs = mutableListOf<String>()
            it.forEach {
                quesIDs.add(it.QuesID)
            }
            viewModel.getResourceQuesInfo(quesIDs.joinToString("|"), 1, true, true)
        })
        viewModel.tkModels.observe(this, Observer {
            loadingDialog.dismiss()
            this.selectedIDs.forEach { classicQues ->
                it.forEachIndexed { index, tkModel ->
                    if (classicQues.QuesID == tkModel.QuesID) {
                        tkModel.IsSelected = true
                    }
                }
            }
            vp_expand.offscreenPageLimit = it.size
            vp_expand.adapter = object : FragmentStateAdapter(supportFragmentManager, lifecycle) {
                override fun getItemCount(): Int {
                    return it.size
                }

                override fun createFragment(position: Int): Fragment {
                    return AddQuesFragment().apply {
                        arguments = bundleOf(
                                Pair("typeNo", it[position].TypeNo), Pair("position", position))
                    }
                }
            }
            vp_expand.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                @SuppressLint("SetTextI18n")
                override fun onPageSelected(position: Int) {
                    tv_type_name.text = it[position].GenreName
                    tv_num.text = "${position + 1}/${it.size}"
                    tv_add.text = if (viewModel.tkModels.value!![position].IsSelected) "取消添加" else "添加"
                    tv_add.tag = if (viewModel.tkModels.value!![position].IsSelected) "remove" else "add"
                }
            })
        })
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> {
                if (selectedIDs.size < 1) {
                    MyToast.showToast( "请至少选择一道典型错题！")
                    return
                }
                val intent = Intent().apply {
                    putParcelableArrayListExtra("ClassicWrongQues", selectedIDs)
                }
                setResult(3, intent)
                finish()
            }
            R.id.tv_add -> {
                val classicWrongQue = viewModel.classicWrongQue.value!![vp_expand.currentItem]
                if (tv_add.tag == "add") {
                    if (selectedIDs.size == 5) {
                        MyToast.showToast( "典型错题至多选择五个！")
                        return
                    }
                    tv_add.tag = "remove"
                    tv_add.text = "取消添加"
                    viewModel.tkModels.value!![vp_expand.currentItem].IsSelected = true
                    selectedIDs.add(classicWrongQue)
                } else {
                    tv_add.tag = "add"
                    tv_add.text = "添加"
                    viewModel.tkModels.value!![vp_expand.currentItem].IsSelected = false
                    selectedIDs.remove(classicWrongQue)
                }
            }
        }
    }
}